package cn.breeze.org.service.impl;

import cn.breeze.basic.service.impl.BaseServiceImpl;
import cn.breeze.basic.utils.MD5Utils;
import cn.breeze.basic.utils.PageList;
import cn.breeze.basic.utils.StrUtils;
import cn.breeze.org.domain.Employee;
import cn.breeze.org.mapper.EmployeeMapper;
import cn.breeze.org.query.EmployeeQuery;
import cn.breeze.org.service.IEmployeeService;
import cn.breeze.system.domain.Role;
import cn.breeze.user.domain.Logininfo;
import cn.breeze.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
// 只读事务
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private LogininfoMapper logininfoMapper;

    @Override
    @Transactional
    public void save(Employee employee) {
        // 初始化employee
        initemployee(employee);
        // 保存employee
        employeeMapper.save(employee);
        // 初始化Logininfo
        Logininfo logininfo = employee2Logininfo(employee);
        // 保存logininfo
        logininfoMapper.save(logininfo);
        // 设置employee标的logininfoId
        employee.setLogininfoId(logininfo.getId());
        // 修改employee
        initRoleMenuPermission(employee);

    }

    /**
    * @Title: employee2Logininfo
    * @Description: 初始化Logininfo
    * @Author: Mr.D
    * @Version: 1.0
    * @Date:  2022/7/6 17:58
    * @Parameters: [employee]
    * @Return void
    */
    private Logininfo employee2Logininfo(Employee employee) {
        Logininfo logininfo = new Logininfo();
        // 拷贝employee到logininfo
        BeanUtils.copyProperties(employee, logininfo);
        // 设置员工类型
        logininfo.setType(0);
        return logininfo;
    }

    /**
    * @Title: initemployee
    * @Description: 初始化employee
    * @Author: Mr.D
    * @Version: 1.0
    * @Date:  2022/7/6 17:45
    * @Parameters: [employee]
    * @Return void
    */
    private void initemployee(Employee employee) {
        String password = employee.getPassword();
        // 获取盐值
        String salt = StrUtils.getComplexRandomString(32);
        // 根据盐值和密码md5加密
        String saltPassword = MD5Utils.encrypByMd5(salt + password);
        // 设置盐值
        employee.setSalt(salt);
        // 设置密码
        employee.setPassword(saltPassword);
    }

    @Override
    @Transactional
    public void update(Employee employee) {
        // 初始化employee
        initemployee(employee);
        // 修改employee
        initRoleMenuPermission(employee);
        // 初始化Logininfo
        Logininfo logininfo = employee2Logininfo(employee);
        // 设置logininfo的主键id
        logininfo.setId(employee.getLogininfoId());
        // 修改logininfo
        logininfoMapper.update(logininfo);
    }

    @Override
    @Transactional
    public void remove(Long id) {
        // 根据id获取员工信息
        Employee employee = employeeMapper.loadById(id);
        // 删除对应的role信息
        employeeMapper.removeEmployeeIdRole(id);
        // 删除员工信息
        employeeMapper.remove(id);
        // 删除对应的Logininfo表的登录信息
        logininfoMapper.remove(employee.getLogininfoId());
    }

    @Override
    public void patchRemove(Long[] ids) {
        for (Long id : ids) {
            remove(id);
        }
    }

    /**
    * @Title: initRoleMenuPermission
    * @Description: 初始化employee
    * @Author: Mr.D
    * @Version: 1.0
    * @Date:  2022/7/13 12:47
    * @Parameters: [employee]
    * @Return void
    */
    public void initRoleMenuPermission(Employee employee){
        Long id = employee.getId();
        if (employee.getId() != null){
            // 根据id删除role和employee中间表信息
            employeeMapper.removeEmployeeIdRole(id);
            // 保存employee
            employeeMapper.update(employee);
        } else {
            // 保存employee
            employeeMapper.save(employee);
        }
        // 保存role
        if (employee.getRoles() != null){
            employeeMapper.saveEmployeeRole(id, employee.getRoles());
        }
    }

    /**
    * @Title: loadByLogininfoId
    * @Description: 根据logininfoId获取店铺id
    * @Author: Mr.D
    * @Version: 1.0
    * @Date:  2022/7/16 21:45
    * @Parameters: [id]
    * @Return cn.breeze.org.domain.Employee
    */
    @Override
    public Employee loadByLogininfoId(Long logininfoId) {
        return employeeMapper.loadByLogininfoId(logininfoId);
    }

    @Override
    public List<Employee> loadByShopId(Long shopId) {
        return employeeMapper.loadByShopId(shopId);
    }
}
